﻿using Abp.Application.Services.Dto;
using Abp.AutoMapper;
using Newtonsoft.Json;
using SingleUserMall.Coupons;
using SingleUserMall.ShoppingCarts;
using SingleUserMall.Workers;
//using SingleUserMall.Workers.VO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SingleUserMall.Orders.VO
{
    /// <summary>
    /// 订单
    /// </summary>
    [AutoMapFrom(typeof(Order))]
    public class OrderVO : EntityDto<long>
    {
        //internal ShippingCategory inShippingCategory { get; set; }
        internal Coupon inCoupon { get; set; }

        internal Worker inWorker { get; set; }

        /// <summary>
        ///  购买理财产赠送品朋友的用户Id
        /// </summary>
        public long? FriendUserId { get; set; }
        /// <summary>
        /// 头像
        /// </summary>
        public string Avatar { get; set; }
        /// <summary>
        /// 朋友头像
        /// </summary>
        public string FriendAvatar { get; set; }
        /// <summary>
        /// 昵称
        /// </summary>
        public string UserName { get; set; }
        /// <summary>
        /// 朋友昵称
        /// </summary>
        public string FriendUserName { get; set; }
        /// <summary>
        /// 订单编号
        /// </summary>
        public string OrderNo { get; set; }

        /// <summary>
        /// 是否砍价订单
        /// </summary>
        public bool IsBargarin { get; set; }
        /// <summary>
        ///  状态（-1：取消，0：未提交，1：待付款，2：已付款，3：已成团，4：已发货，5：已收货，6：已评价）
        /// </summary>
        public OrderStatus Status { get; set; }
        /// <summary>
        /// 状态名称
        /// </summary>
        public string StatusView => Status.GetDescription();
        /// <summary>
        ///  状态（1：待付款，2：待成团，3：待发货，4：待收货，5：待提货，6：待评价）
        /// </summary>
        public OrderWaitStatus WaitStatus
        {
            get
            {
                switch (Status)
                {
                    case OrderStatus.WaitPay:
                        return OrderWaitStatus.WaitPay;
                    case OrderStatus.Group:
                        return OrderWaitStatus.WaitSend;
                    case OrderStatus.Paid:
                        return IsGroup ? OrderWaitStatus.WaitGroup : OrderWaitStatus.WaitSend;
                    case OrderStatus.Sent:
                        return ShippingType == Orders.ShippingType.Get ? OrderWaitStatus.WaitLift : OrderWaitStatus.WaitGet;
                    case OrderStatus.Successed:
                        return OrderWaitStatus.WaitAppraise;
                    case OrderStatus.Invalidation:
                        return OrderWaitStatus.Invalidation;
                    default:
                        return OrderWaitStatus.WaitPay;
                }
            }
        }
        /// <summary>
        /// 是否团购订单
        /// </summary>
        public bool IsGroup { get; set; }
        /// <summary>
        /// 支付类型
        /// </summary>
        public PayType PayType { get; set; }
        /// <summary>
        /// 支付类型名称
        /// </summary>
        public string PayTypeView => PayType.GetDescription();
        /// <summary>
        /// 送货方式 （0：3小时快速达，1：1小时极速达（加急费￥10），2：到店自取）
        /// </summary>
        public ShippingType? ShippingType { get; set; }
        /// <summary>
        /// 购物车订单（0：商品购物车订单，1：购买树订单）
        /// </summary>
        public ShoppingCartType? ShoppingCartType { get; set; }
        /// <summary>
        /// 送货方式名称
        /// </summary>
        public string ShippingTypeView => ShippingType?.GetDescription();
        /// <summary>
        /// 合计费用
        /// </summary>
        public decimal Amount { get; set; }
        /// <summary>
        /// 实际费用
        /// </summary>
        public decimal ActualAmount { get; set; }

        /// <summary>
        /// 运费
        /// </summary>
        public decimal Freight { get; set; }
        /// <summary>
        /// 包装费
        /// </summary>
        public decimal PackPrice { get; set; }
        /// <summary>
        /// 是否选中包装费
        /// </summary>
        public bool IsPackPrice { get; set; }

        /// <summary>
        /// 收货人
        /// </summary>
        public string Receiver { get; set; }
        /// <summary>
        /// 收货人电话
        /// </summary>
        public string ReceiverPhone { get; set; }
        /// <summary>
        /// 收货人地址
        /// </summary>
        public string ReceiverAddress { get; set; }
        /// <summary>
        /// 邮政编码
        /// </summary>
        public string PostalCode { get; set; }
        ///// <summary>
        ///// 配送方式id
        ///// </summary>
        //public int ShippingCategoryId { get; set; }
        //[ForeignKey("ShippingCategoryId")]
        //public virtual ShippingCategory ShippingCategory { get; set; }
        /// <summary>
        /// 买家留言
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 支付时间
        /// </summary>
        public DateTime? PayTime { get; set; }
        /// <summary>
        /// 分红结束时间
        /// </summary>
        public DateTime? DividendEndTime { get; set; }
        /// <summary>
        /// 发货时间
        /// </summary>
        public DateTime? ShippingTime { get; set; }
        /// <summary>
        /// 收货时间
        /// </summary>
        public DateTime? ReceiveTime { get; set; }
        /// <summary>
        /// 快递单号
        /// </summary>
        public string ShippingNo { get; set; }
        /// <summary>
        /// 物流消息
        /// </summary>
        public string ShippingLog { get; set; }
        /// <summary>
        /// 是否是朋友地址
        /// </summary>
        public bool IsFriendAddress { get; set; }
        /// <summary>
        /// 物流信息详情
        /// </summary>
        public ExpressResponse ShippingLogInfo
        {
            get
            {
                try
                {
                    return JsonConvert.DeserializeObject<ExpressResponse>(ShippingLog);
                }
                catch
                {
                    return null;
                }
            }
        }
        /// <summary>
        /// 优惠券金额
        /// </summary>
        public decimal CouponAmount
        {
            get
            {
                try
                {
                    if (inCoupon != null)
                    {
                        return inCoupon.Amount;
                    }

                    else
                    {
                        return 0;
                    }
                }
                catch
                {
                    return 0;
                }
            }
        }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreationTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndDateTime { get; set; }
        /// <summary>
        /// 订单商品
        /// </summary>
        public IReadOnlyList<OrderDetailVO> OrderDetails { get; set; }
        /// <summary>
        /// 支付参数
        /// </summary>
        public string PayParams { get; set; }
        /// <summary>
        /// 所有收益归朋友
        /// </summary>
        public bool AllBenefitForFriend { get; set; }
        /// <summary>
        /// 购买最小果树号
        /// </summary>
        public string MinTreeNumber { get; set; }
        /// <summary>
        /// 购买最大果树号
        /// </summary>
        public string MaxTreeNumber { get; set; }
        /// <summary>
        /// 树号范围
        /// </summary>
        public string TreeNumberRange { get; set; }
    }
}
